<template>
  <div>
    <div class="top">
      <img src="..\..\public\images\返回.png" alt="" @click="goBack">
      <span>所有评价</span>
    </div>
    <div class="pf">综合评分<span>{{ averageScore }}</span></div>
    <div v-if="comments && comments.length > 0">

      <div class="bq">
        <div v-for="(topRateTags, index) in topRateTags" :key="index" v-show="index > 0">
          <div class="top-bq"> {{ topRateTags.value }} ({{ topRateTags.num }})</div>
        </div>
      </div>

      <div class="bq">
        <div v-for="(rateTags, index) in rateTags" :key="index" v-show="index > 0">
          <div class="btton-bq"> {{ rateTags.value }} {{ rateTags.num }}</div>
        </div>
      </div>

      <div class="xian-pl"></div>


      <ul>
        <li v-for="(comment, index) in comments" :key="index">
          <div class="user">
            <van-image round width="40rem" height="40rem" :src="comment.user.avatar" />
            <div class="user-name">
              <p class="name"> {{ comment.user.uname }}</p>
              <p class="style"> {{ comment.style }}</p>
            </div>
          </div>
          <p class="cotent">{{ comment.content }}</p>


          <div v-if="comment.images && comment.images.length > 0">
            <div class="image-row">
              <div v-for="(img, imgIndex) in comment.images" :key="imgIndex" class="image-col">
                <van-image
                  width="110rem"
                  height="110rem"
                  fit="cover"
                  :src="img"
                  :alt="`评论图片${imgIndex}`"
                  @click="showImagePreview(imgIndex, comment.images)"
                />
              </div>
            </div>
            <p class="time">{{ comment.time }}</p>
          </div>
          <div class="xian-pl"></div>

        </li>
      </ul>
    </div>
    <p v-else>正在加载评论...</p>
  </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';
import { ref, onMounted } from 'vue';
import { ImagePreview } from 'vant';

const route = useRoute();
const router = useRouter();

const receivedIid = route.params.iid;
console.log('接收到的iid参数:', receivedIid);

// 定义响应式数据用来存放评论列表
const comments = ref<any[]>([]);
// 定义响应式数据用于存储平均评分
const averageScore = ref('');
const topRateTags = ref<any[]>([]);
const rateTags = ref<any[]>([]);

// 调用API获取评论数据的函数
const fetchComments = async () => {
  try {
    const response = await axios.get(`https://apis.netstart.cn/mogujie/goods/commonRateList?itemId=${receivedIid}`);
    const { data } = response;
    if (data.ret === 'SUCCESS') {
      comments.value = data.data.list;
      // 获取平均评分并赋值给averageScore
      averageScore.value = data.data.averageScore;
      rateTags.value = data.data.rateTags;
      topRateTags.value = data.data.topRateTags;
    } else {
      console.error('获取评论数据失败:', data);
    }
  } catch (error) {
    console.error('请求评论数据出错:', error);
  }
};

const goBack = () => {
  router.back();
};

const showImagePreview = (index: number, imgUrls: string[]) => {
  ImagePreview({
    images: imgUrls,
    startPosition: index,
    closeable: true,
    closeIcon: 'close',
  });
};

// 在组件挂载后请求评论数据
onMounted(() => {
  fetchComments();
});
</script>

<style lang="less" scoped>
.xian-pl {
  width: 100%;
  height: 20rem;
  background-color: #f6f6f6;
  margin-top: 10rem;
}

.top {
  height: 45rem;
  width: 100%;
  color: #5e5e5e;
  display: flex;
  align-items: center;
  font-size: 17rem;
  border-bottom: 1px solid #9999999a;
  //固定在顶部
  position: fixed;
  top: 0;
  background-color: #fdfbfb;
  z-index: 100;


  span {
    text-align: center;
    margin: 0 auto;
  }

  img {
    width: 20rem;
    height: 20rem;
    margin-left: 10rem;
    position: absolute;
  }
}

.pf {
  font-size: 15rem;
  color: #666666;
  background-color: #fef0f0;
  height: 40rem;
  line-height: 40rem;
  padding-left: 10rem;
  margin-top: 45rem;

  span {
    color: #946515;
  }
}

.bq {
  font-size: 12rem;
  display: flex;
  flex-wrap: wrap;

  //
  &::-webkit-scrollbar {
    display: none;
  }

  margin-left: 10rem;

 .top-bq {
    color: #333;
    background-color: #f6f6f6;
    padding: 0 10rem;
    border-radius: 50rem;
    margin-top: 10rem;

  }

 .btton-bq {
    color: #333;
    background-color: #fff6f6;
    padding: 5rem 10rem;
    margin-top: 10rem;
    margin-right: 10rem;
  }
}

.image-row {
  display: flex;
  flex-wrap: wrap;
}

.image-col {
  box-sizing: border-box;
  padding: 5rem;
  margin-left: 5rem;
}

.user {
  display: flex;
  align-items: center;
  margin-left: 10rem;
 .user-name {
    margin-left: 10rem;
   .name {
      font-size: 15rem;
      font-weight: bold;
      margin-bottom: 0;
    }
   .style {
      font-size: 12rem;
      color: #999;
      margin-top: 3rem;
    }
  }
}

.cotent {
  font-size: 12rem;
  color: #333;
  margin-left: 10rem;
  margin-top: 0rem;
}

.time {
  font-size: 12rem;
  color: #999;
  margin-left: 10rem;
  margin-top: 5rem;
}
</style>
